﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.pro2e.console.core.hibernate;

/// <summary>
/// ConfigHelper 的摘要描述
/// 為取的在Web.config appSettings的參數
/// </summary>
/// 
namespace com.pro2e.web.config
{
    public class ConfigHelper
    {

        /// <summary>
        /// SMTP Server
        /// </summary>
        public string SMTPHost { get; set; }
        /// <summary>
        /// SMTP Server
        /// </summary>
        public string SMTPUser { get; set; }
        /// <summary>
        /// SMTP Server
        /// </summary>
        public string SMTPPassword { get; set; }
        /// <summary>
        /// 系統發信寄件人
        /// </summary>
        public string ServiceEmail { get; set; }
        /// <summary>
        ///   匯入excel實體位置
        /// </summary>
        public string ImportExcelPath { get; set; }
          /// <summary>
        ///   上傳圖片相對位置
        /// </summary>
        public string UploadImageVirtualPath { get; set; }
        



        /// <summary>
        /// 是否專案測試中Y:測試 N:正式
        /// 在測試中，寄mail測試只會到測試信箱 不會到user手中
        /// </summary>
        public string OnTest { get; set; }

        /// <summary>
        /// 測試收件者Email
        /// </summary>
        public string TestRecMail { get; set; }

        /// <summary>
        /// 網站在機器的實體位置
        /// </summary>
        public string WebLocation { get; set; }

        /// <summary>
        /// XMLdata在機器的實體位置
        /// </summary>
        public string XmlDataPath { get; set; }

        /// <summary>
        /// Images在機器的實體位置
        /// </summary>
        public string ImagesPath { get; set; }

        public string ESHOP_ID { get; set; }

        /// <summary>
        /// 網址
        /// </summary>
        public string Host { get; set; }



        public ConfigHelper()
        {

            SMTPHost = ConfigurationManager.AppSettings["SMTPHost"];
            ServiceEmail = ConfigurationManager.AppSettings["ServiceEmail"];
            Host = ConfigurationManager.AppSettings["Host"];
            ImportExcelPath = ConfigurationManager.AppSettings["ImportExcelPath"];
            UploadImageVirtualPath = ConfigurationManager.AppSettings["UploadImageVirtualPath"];
            SMTPPassword = ConfigurationManager.AppSettings["SMTPPassword"];
            SMTPUser = ConfigurationManager.AppSettings["SMTPUser"];
            //TestRecMail = ConfigurationManager.AppSettings["TestRecMail"];
            //MerchanId = ConfigurationManager.AppSettings["MerchanId"];
            //TerminalId = ConfigurationManager.AppSettings["TerminalId"];
            //LinkURL = ConfigurationManager.AppSettings["LinkURL"];
            //NotifyURL = ConfigurationManager.AppSettings["NotifyURL"];
            //WebLocation = ConfigurationManager.AppSettings["WebLocation"];
            //XmlDataPath = ConfigurationManager.AppSettings["XmlDataPath"];
            //ImagesPath = ConfigurationManager.AppSettings["ImagesPath"];
            //ESHOP_ID = ConfigurationManager.AppSettings["EShopId"];            
        }

        /// <summary>
        /// 傳回資料庫相關的設定陣列
        /// 索引值為0:資料庫位置1:帳號2:密碼3:資料庫名稱
        /// </summary>
        /// <returns></returns>
        public String[] GetDbAccessInfo()
        {
            string[] dbInfo = new string[4];
            string dbLocation = "", dbUser = "", dbPassword = "", dbName = "";
            string conn = NHibernateSessionManager.Instance.GetSession().Connection.ConnectionString;


            string[] connArray = conn.Split(';');


            foreach (string str in connArray)
            {
                if (str.IndexOf("Data Source") != -1)
                {
                    dbLocation = str.Split('=')[1].Trim();

                    dbLocation = dbLocation.Replace("\\", "/");
                }
                else if (str.IndexOf("User ID") != -1)
                {
                    dbUser = str.Split('=')[1].Trim();
                }
                else if (str.IndexOf("Password") != -1)
                {
                    dbPassword = str.Split('=')[1].Trim();
                }
                else if (str.IndexOf("Initial Catalog") != -1)
                {
                    dbName = str.Split('=')[1].Trim();
                }
            }

            dbInfo[0] = dbLocation;
            dbInfo[1] = dbUser;
            dbInfo[2] = dbPassword;
            dbInfo[3] = dbName;
            return dbInfo;
        }

        /// <summary>
        /// 取得網址
        /// </summary>
        /// <returns></returns>
        public string GetWebHost()
        {
            return string.Format("http://{0}/", Host);
        }
    }
}